﻿using System;
using Blockey.ScreenManagers;
using BlockeyEssentials;
using BlockeyEssentials.Drawing;
using BlockeyEssentials.Input;
using BlockeyEssentials.ScreenSystem;
using BlockeyEssentials.ScreenSystem.ScreenTypes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Blockey.MenuSystemScreens
{
    public class CreateMenuScreen : MenuScreen
    {
        public CreateMenuScreen(ScreenManager oManager)
            : base(oManager, "Create Menu")
        {
            for (MenuItem oOption = MenuItem.New; oOption <= MenuItem.Back; oOption++)
            {
                _Options.Add(oOption.ToString().ToUpper());
            }

            int oWidth = 700;
            int oBuffer = 60;
            int oInsideBuffer = 20;
            var oBackHeight = 100;

            _TextFont = new Font(All.Font_Debug);
            _TextFont.Color = Color.Black;
            _TextFont.Layer = 95;

            _AttemptScreen = null;

            var oBounds = new Rectangle(
                All.Resolution.Center.X - (oWidth / 2) - oInsideBuffer,
                oBuffer,
                oWidth,
                All.Resolution.Height - (oBuffer * 2));

            var oShape = new Rectangle(
                oBounds.Left + oInsideBuffer,
                oBounds.Top + oInsideBuffer,
                (oBounds.Width - oInsideBuffer) / 2,
                oBounds.Height - (oInsideBuffer * 3) - oBackHeight);

            //-------- New Button --------//
            _CreateButton = new MenuButton(oShape, Color.Red, 80, _TextFont, "New");

            //-------- Load Button --------//
            oShape.X = _CreateButton.Bounds.Right + oInsideBuffer;
            _LoadButton = new MenuButton(oShape, Color.LightSalmon, 80, _TextFont, "Load");

            //-------- Back Button --------//
            oShape.Y = oShape.Bottom + oInsideBuffer;
            oShape.Width = oBounds.Width;
            oShape.X = oBounds.Left + oInsideBuffer;
            oShape.Height = oBackHeight;
            _BackButton = new MenuButton(oShape, Color.Crimson, 80, _TextFont, "Back");

            _SelectedItemSprite = new SpriteR(All.Texture_Square, _CreateButton.Bounds);
            _SelectedItemSprite.Color = Color.Yellow;
            _SelectedItemSprite.Layer = 90;
        }

        #region Methods

        public override void Update()
        {
            if (Active)
            {
                if (_AttemptScreen == null)
                {
                    Input();
                }
            }
        }

        public override void Draw(SpriteBatch oSB)
        {
            _CreateButton.Draw(oSB, Transition);
            _LoadButton.Draw(oSB, Transition);
            _BackButton.Draw(oSB, Transition);
            _SelectedItemSprite.Draw(oSB, Transition);
        }

        protected override void Select()
        {
            switch ((MenuItem)_Index)
            {
                case MenuItem.New:
                    {
                        var oScreen = new EditTeamInfoScreen(_Manager, true);
                        GoTo(oScreen);
                        break;
                    }
                case MenuItem.Load:
                    {
                        //var oScreen = new LoadTeamScreen(_Manager);
                        //GoTo(oScreen);
                        break;
                    }
                case MenuItem.Back:
                    {
                        Cancel();
                        break;
                    }
            }
        }

        protected override void Cancel()
        {
            var oScreen = new PlayMenuScreen(_Manager);
            GoTo(oScreen);
        }

        protected override void IndexTo(int oIndex)
        {
            base.IndexTo(oIndex);
            ChangeSelectedHighlight();
        }

        #region Helper Methods

        private void Input()
        {
            var oUp = false;
            var oDown = false;
            var oLeft = false;
            var oRight = false;
            var oSelect = false;
            var oCancel = false;

            var oKeyboardInput = InputManager.Keyboard;
            if (oKeyboardInput.JustPressed(Keys.Up))
                oUp = true;
            if (oKeyboardInput.JustPressed(Keys.Down))
                oDown = true;
            if (oKeyboardInput.JustPressed(Keys.Left))
                oLeft = true;
            if (oKeyboardInput.JustPressed(Keys.Right))
                oRight = true;
            if (oKeyboardInput.JustPressed(Keys.Enter, Keys.F))
                oSelect = true;
            if (oKeyboardInput.JustPressed(Keys.D))
                oCancel = true;

            var oGamePadInput = InputManager.GamePad1;
            if (oGamePadInput.JustPressed(Buttons.DPadUp))
                oUp = true;
            if (oGamePadInput.JustPressed(Buttons.DPadDown))
                oDown = true;
            if (oGamePadInput.JustPressed(Buttons.DPadLeft))
                oLeft = true;
            if (oGamePadInput.JustPressed(Buttons.DPadRight))
                oRight = true;
            if (oGamePadInput.JustPressed(Buttons.A))
                oSelect = true;
            if (oGamePadInput.JustPressed(Buttons.B))
                oCancel = true;

            if (oUp)
            {
                var oIndex = (MenuItem)_Index;
                if (oIndex == MenuItem.Back)
                {
                    IndexTo((int)_LastIndex);
                    return;
                }
            }

            if (oDown)
            {
                var oIndex = (MenuItem)_Index;
                if (oIndex != MenuItem.Back)
                {
                    _LastIndex = oIndex;
                    IndexTo((int)MenuItem.Back);
                    return;
                }
            }

            if (oRight)
            {
                var oIndex = (MenuItem)_Index;
                if (oIndex == MenuItem.New)
                {
                    IndexUp();
                    return;
                }
            }
            if (oLeft)
            {
                var oIndex = (MenuItem)_Index;
                if (oIndex == MenuItem.Load)
                {
                    IndexDown();
                    return;
                }
            }

            if (oSelect)
            {
                Select();
                return;
            }

            if (oCancel)
            {
                Cancel();
                return;
            }

        }

        private void ChangeSelectedHighlight()
        {
            switch ((MenuItem)_Index)
            {
                case MenuItem.New: _SelectedItemSprite.Rectangle = _CreateButton.Bounds;
                    break;
                case MenuItem.Load: _SelectedItemSprite.Rectangle = _LoadButton.Bounds;
                    break;
                case MenuItem.Back: _SelectedItemSprite.Rectangle = _BackButton.Bounds;
                    break;
            }
        }

        #endregion

        #endregion

        private MenuButton _CreateButton;
        private MenuButton _LoadButton;
        private MenuButton _BackButton;
        private SpriteR _SelectedItemSprite;
        private MenuItem _LastIndex;
        private Font _TextFont;

        private Screen _AttemptScreen;

        public enum MenuItem
        {
            New,
            Load,
            Back
        }

    }
}
